package com.xin.di.uav.applet.controller;

import com.xin.di.uav.applet.resp.*;
import com.xin.di.uav.applet.service.IOrderInfoService;
import com.xin.di.uav.common.annotate.DistributedLock;
import com.xin.di.uav.common.annotate.ReSubmit;
import com.xin.di.uav.applet.service.OrderService;
import com.xin.di.uav.applet.rest.OrderRest;
import com.xin.di.uav.common.request.order.UpdateStatusBySnRequest;
import com.xin.di.uav.operate.rest.UserRest;
import com.xin.di.uav.common.utils.RedisKeyConstants;
import com.xin.di.uav.common.request.common.IdRequest;
import com.xin.di.uav.common.request.order.CheckOrderRequest;
import com.xin.di.uav.common.request.order.RescuePrepareOrderRequest;
import com.xin.di.uav.common.request.order.UpdateStatusRequest;
import com.xin.di.uav.common.response.user.UserResponse;
import com.xin.di.uav.common.utils.Rs;
import com.xin.di.uav.user.order.service.CommonOrderService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * @author 01430312
 */
@RestController
@RequestMapping("/sys/order/")
public class OrderController {

    @Autowired
    private UserRest userRest;

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderRest orderRest;



    @Autowired
    private CommonOrderService commonOrderService;

    @GetMapping("get-user-info")
    public Rs<UserResponse> getByUserId() {
        return Rs.success(userRest.getByUserId());
    }


    /**
     * 下单接口
     *
     * @return
     */
    @PostMapping("place-order")
    @Operation(tags = "提交订单", description = "提交订单")
    @ReSubmit(key = RedisKeyConstants.placeOrder, expire = 3)
    @DistributedLock(key = RedisKeyConstants.placeOrder, expireTime = 5, userDimension = true)
    public Rs<Map<String, String>> placeOrder(@Valid @RequestBody RescuePrepareOrderRequest rescuePrepareOrderRequest) {
        // 调用下单接口
        return Rs.success(orderService.placeOrder(rescuePrepareOrderRequest));
    }


    /**
     * 购物车变更检查订单
     *
     * @return
     */
    @PostMapping("check-order")
    @Operation(tags = "购物车变更检查订单", description = "购物车变更检查订单")
    public Rs<CheckOrderResponse> checkOrder(@RequestBody CheckOrderRequest checkOrderRequest) {
        return Rs.success(orderService.checkOrder(checkOrderRequest));
    }

    @PostMapping("cancel-order")
    @Operation(tags = "取消订单", description = "取消订单")
    @CrossOrigin
    @ReSubmit(key = RedisKeyConstants.appCancelOrder, expire = 3)
    @DistributedLock(key = RedisKeyConstants.appCancelOrder, expireTime = 5, userDimension = true)
    public Rs<Date> cancelOrder(@RequestBody @javax.validation.Valid IdRequest idRequest) {
        return Rs.success(commonOrderService.cancelOrder(idRequest.getId()));
    }

    @GetMapping("get-order-list")
    @Operation(tags = "获取订单列表", description = "获取订单列表")
    public Rs<OrderListResponse> getOrderList(@RequestParam Integer page, @RequestParam Integer size) {
        return Rs.success(orderService.getOrderList(page, size));
    }



    @PostMapping("update-status")
    @CrossOrigin
    public Rs<Void> updateStatus(@Valid @RequestBody UpdateStatusRequest updateStatusRequest) {
        orderRest.updateStatus(updateStatusRequest);
        return Rs.success();
    }





    @GetMapping("get-pay-page-info")
    @Operation(tags = "获取支付订单信息接口", description = "获取支付订单信息接口")
    public Rs<PayPageInfo> getPayPageInfo() {
        return Rs.success(orderService.getPayPageInfo());
    }

    @GetMapping("get-orders-detail")
    @Operation(tags = "获取订单总条数", description = "获取订单总条数")
    public Rs<OrderDetailResp> getOrderDetail(@RequestParam String orderSn) {
        return Rs.success(orderService.getOrderDetail(orderSn));
    }

}
